function getAbbreviations() {

  var result = new Array();
  var abbr = document.getElementsByTagName("abbr");

  for (var i = 0; i < abbr.length; i++) {
    result[abbr[i].lastChild.nodeValue] = abbr[i].getAttribute("title");
  }

  return result;
}


function insertAbbreviations(result) {

  var dl = document.createElement("dl");

  for (key in result) {
    var dt = document.createElement("dt");
    dt.appendChild(document.createTextNode(key));
    var dd = document.createElement("dd");
    dd.appendChild(document.createTextNode(result[key]));

    dl.appendChild(dt);
    dl.appendChild(dd);
  }

  var header = document.createElement("h2");
  header.appendChild(document.createTextNode("Abbreviations"));

  document.body.appendChild(header);
  document.body.appendChild(dl);
}


function displayCitations() {

  var quotes = document.getElementsByTagName("blockquote");

  for (var i = 0; i < quotes.length; i++) {
    if(!quotes[i].getAttribute("cite")) {
      continue;
    } else {
      var url = quotes[i].getAttribute("cite");
      var link = document.createElement("a");
      link.appendChild(document.createTextNode("source"));
      link.setAttribute("href", url);

      var superscript = document.createElement("sup");
      superscript.appendChild(link);

      var quoteChildren = quotes[i].getElementsByTagName("*");
      var elem = quoteChildren[quoteChildren.length - 1];
      elem.appendChild(superscript);
    }
  }
}


window.onload = function() {

  insertAbbreviations(getAbbreviations());
  displayCitations();
}
